//
//  YWDownloadStatus.m
//  videoTest3
//
//  Created by jun peng on 2022/7/20.
//

#import "YWDownloadStatus.h"

@implementation YWDownloadStatus

-(NSString *)fullPath{
    
    if (_fullPath == nil) {
        
        //创建一个在正在下载文件夹里的文件路径
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyyMMddHHmmss";
        int x = arc4random() % 100;
        NSString *dateStr = [formatter stringFromDate:[NSDate date]];
        NSString *fileName = [NSString stringWithFormat:@"%@%d.mp4",dateStr,x];
        
        NSString *path = [[YWDownloadStatus createDowloadingFolder] stringByAppendingPathComponent:fileName];
        
        _fullPath = path;
        
    }
    return _fullPath;
}
//创建正在下载文件夹
+(NSString *)createDowloadingFolder{
    
    NSError *error = nil;
    if (![[NSFileManager defaultManager] fileExistsAtPath:YWDownloadingCachepath]) {
         [[NSFileManager defaultManager] createDirectoryAtPath:YWDownloadingCachepath
                                   withIntermediateDirectories:NO
                                                    attributes:nil
                                                         error:&error];
    }
    return YWDownloadingCachepath;
}

//先保证它不为0因为什么数除以0都是无限大
-(NSInteger)totalSize{
    if (_totalSize == 0) {
        _totalSize = 1;
    }
    return _totalSize;
}


//- - - - - - - - 关于时间的设置与处理方便排序下载模型数组 - - - - - - - -

+ (NSInteger)compareDate:(NSString*)aDate withDate:(NSString*)bDate
{
    NSInteger aa = 0;
    NSDateFormatter *dateformater = [[NSDateFormatter alloc] init];
    [dateformater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dta = [[NSDate alloc] init];
    NSDate *dtb = [[NSDate alloc] init];
    
    dta = [dateformater dateFromString:aDate];
    dtb = [dateformater dateFromString:bDate];
    NSComparisonResult result = [dta compare:dtb];
    if (result==NSOrderedSame)
    {
        //相等
        aa = 0;
    }else if (result==NSOrderedAscending) {
        //bDate比aDate大
        aa = 1;
    }else if (result==NSOrderedDescending) {
        //bDate比aDate小
        aa = -1;
    }
    return aa;
}

+ (NSString *)dateToString:(NSDate *)date
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *strDate = [dateFormatter stringFromDate:date];
    return strDate;
}

-(void)setJoinDownloadDate:(NSString *)joinDownloadDate{
    
    //是空你才能赋值，否则你就不能赋值了，之后时间会作为归档保存起来，即只能设置一次加入下载时间
    if (_joinDownloadDate == nil) {
        _joinDownloadDate = joinDownloadDate;
        
    }
    
}

//时间戳
-(NSInteger)stampTime{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];//使用系统的时区
    [formatter setTimeZone:timeZone];
    NSDate* date = [formatter dateFromString:_joinDownloadDate]; //------------将字符串按formatter转成nsdate
    return (long)[date timeIntervalSince1970];
}

//MARK将模型数组按时间降序排序
+ (NSArray *)compareDateWithArray:(NSArray *)dataArray
{
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"stampTime" ascending:YES];
    // 排序结果
    return [dataArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

}


//归档反归档
MJCodingImplementation



@end
